package com.zhuoshi.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhuoshi.entity.Deadline;
import com.zhuoshi.entity.PayMentTypeCustom;
import com.zhuoshi.entity.PayMentTypeQueryVo;
import com.zhuoshi.entity.Paymenttype;
import com.zhuoshi.mapper.DeadlineMapper;
import com.zhuoshi.mapper.PaymenttypeMapper;
import com.zhuoshi.service.PayMentTypeService;

@Service("payMentTypeService")
@Transactional
public class PayMentTypeServiceImpl implements PayMentTypeService{
	@Autowired
	private PaymenttypeMapper paymenttypeMapper;
	@Autowired
	private DeadlineMapper deadlineMapper;
//	查询所有付款方式
	public List<PayMentTypeCustom> selectPayMentTypeList() throws Exception {
		return paymenttypeMapper.selectPayMentTypeList();
	}
//  删除付款方式（单个和批量都可以）
	public Integer deletePayMentTypeList(Integer[] ids) throws Exception {
		Integer deleteByPrimaryKey=null;
		for (Integer id : ids) {
			deleteByPrimaryKey = paymenttypeMapper.deleteByPrimaryKey(id);
		}
		return deleteByPrimaryKey;
	}
//	添加付款方式
	public Integer addPayMentType(PayMentTypeQueryVo payMentTypeQueryVo) throws Exception {
		deadlineMapper.addDeadline(payMentTypeQueryVo.getPayMentTypeCustom().getDeadline());
		Deadline deadline=deadlineMapper.selectDeadlineIdByTime(payMentTypeQueryVo.getPayMentTypeCustom().getDeadline());
		Integer deadlineid=deadline.getId();
		payMentTypeQueryVo.getPayMentTypeCustom().setDeadlineid(deadlineid);
		return paymenttypeMapper.addPayMentType(payMentTypeQueryVo);
	}
	
//	根据ID查付款方式
	public PayMentTypeCustom selectPayMentTypeById(Integer id) throws Exception {
		Paymenttype paymenttype = paymenttypeMapper.selectByPrimaryKey(id);
		PayMentTypeCustom payMentTypeCustom=new PayMentTypeCustom();
		BeanUtils.copyProperties(paymenttype, payMentTypeCustom);
		Deadline deadline = deadlineMapper.selectByPrimaryKey(payMentTypeCustom.getDeadlineid());
		payMentTypeCustom.setDeadline(deadline);
		return payMentTypeCustom;
	}
//	修改付款
	public void  updatePayMentType(PayMentTypeQueryVo payMentTypeQueryVo) throws Exception {
		int updateByPrimaryKey = deadlineMapper.updateByPrimaryKey(payMentTypeQueryVo.getPayMentTypeCustom().getDeadline());
		paymenttypeMapper.updatePayMentType(payMentTypeQueryVo);
	}
//	根据多个查询房间并接入付款方式
	public List<PayMentTypeCustom> selectPayMentTypeByIds(Integer[] ids) throws Exception {
		List<PayMentTypeCustom> payMentTypeCustoms=new ArrayList<PayMentTypeCustom>();
		for(int i=0;i<ids.length;i++){
			if(i==ids.length-1&&ids[i]==null){
				break;
			}
			payMentTypeCustoms.add(paymenttypeMapper.selectPayMentTypeByIds(ids[i]));
		}
		return payMentTypeCustoms;
	}

}
